package com.example.spring_trans.controller;

import com.example.spring_trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/25
 * Time: 13:52
 */
@RestController
@RequestMapping("/trans")
@Slf4j
public class TransactionalController {

    @Autowired
    private UserService userService;

    /**
     * 不加事务的情况下，数据插入成功
     * @param name
     * @param password
     * @return
     */
//    @Transactional
    @RequestMapping("/r1")
    public String registry(String name, String password) {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }
        int a = 10 / 0;
        return "注册成功";
    }

    /**
     * 添加事物的情况，事务回滚
     */
    @Transactional
    @RequestMapping("/r2")
    public String registry2(String name, String password) {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }
        int a = 10 / 0;
        return "注册成功";
    }

    /**
     * 异常被捕获：插入数据成功
     */
    @Transactional
    @RequestMapping("/r3")
    public String registry3(String name, String password) {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }
        try {
            int a = 10 / 0;
        } catch(Exception e) {
            log.error("发生异常");
        }
        return "注册成功";
    }

    /**
     * 异常被捕获，但又抛出异常：事务回滚
     */
    @Transactional
    @RequestMapping("/r4")
    public String registry4(String name, String password) {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }
        try {
            int a = 10 / 0;
        } catch(Exception e) {
            log.error("发生异常");
            throw e;
        }
        return "注册成功";
    }

    /**
     * 手动回滚事务
     */
    @Transactional
    @RequestMapping("/r5")
    public String registry5(String name, String password) {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }
        try {
            int a = 10 / 0;
        } catch(Exception e) {
            log.error("发生异常");
            // 手动回滚事务，基于 AOP 实现
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }


    /**
     * 发生非运行时异常: 事务提交
     */
    @Transactional
    @RequestMapping("/r6")
    public String registry6(String name, String password) throws IOException {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }

        if (true) {
            throw new IOException();
        }

        return "注册成功";
    }

    /**
     * 设置 rollbackFor 发生非运行时异常: 事务回滚
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r7")
    public String registry7(String name, String password) throws IOException {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }

        if (true) {
            throw new IOException();
        }

        return "注册成功";
    }

    /**
     * 设置 rollbackFor 发生非运行时异常: 事务回滚
     */
    @Transactional(rollbackFor = NullPointerException.class)
    @RequestMapping("/r8")
    public String registry8(String name, String password) {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }

        if (true) {
            throw new RuntimeException();
        }

        return "注册成功";
    }

    /**
     * 设置事务的隔离级别 isolation 枚举类
     * DEFAULT: 和使用的数据库保持一致
     * READ_UNCOMMITTED(1): 读未提交
     * READ_COMMITTED(2): 读已提交
     * REPEATABLE_READ(4): 可重复读
     * SERIALIZABLE(8): 序列化
     */
    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r9")
    public String registry9(String name, String password) {
        // 用户注册
        Integer ret = userService.registryUser(name, password);
        if (ret == 1) {
            log.info("数据插入成功，影响行数: " + ret);
        }

        if (true) {
            throw new RuntimeException();
        }

        return "注册成功";
    }

}
